package org.agpsoft.simpledatareceiver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.agpsoft.datareceiverinfrastructure.builder.IDataReceiverMainHandler;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverBuildException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverCloseException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverOpenException;

public class SimpleDataReceiver {

	public static void main(String[] args) {
		try {
			IDataReceiverMainHandler handler = 
				new SimpleBuilder().build();
			
			SimpleDataReceiverContext context = 
				new SimpleDataReceiverContext();
			
			try {
				context.setInputStream( new FileInputStream( args[0] ) );
				context.setOutputStream( new FileOutputStream( args[1] ) );
				context.setRawRecordStream( new FileOutputStream( args[2] ) );
				context.setDecodedRecordStream( new FileOutputStream( args[3] ) );
			} catch (FileNotFoundException exc) {
				exc.printStackTrace();
			}
			
			try {
				handler.open(context);
			} catch (DataReceiverOpenException exc) {
				exc.printStackTrace();
			}
			
			for( int index = 0 ; index < 3; index++ )
			{
				try {
					handler.handle();
				} catch (DataReceiverException e) {
					e.printStackTrace();
				}
			}
			
			try {
				handler.close();
			} catch (DataReceiverCloseException exc) {
				exc.printStackTrace();
			}
			
		} catch (DataReceiverBuildException e) {
			e.printStackTrace();
		}
		
	}

}
